package com.jy.it.ui.admin;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.jy.it.entity.Admin;
import com.jy.it.service.UserService;

@Controller
public class AdminInputDriverController {
	@Autowired
	private UserService userService=null;
	@RequestMapping(value="/admin/input/Driver",method=RequestMethod.GET)
	public String login() {
		return "admin/admin_input_driver";
	}
	@RequestMapping(value="/admin/input/Driver",method=RequestMethod.POST)
	public String add(String username
			,String userno
			,String userdriverno
			,String userpwd
			,MultipartFile photoFile
			,String userphone
			,String useremail
			,String useraddress
			,HttpServletRequest req
			,HttpSession session
			,Model m) throws IllegalStateException, IOException {
		Admin admin=(Admin) session.getAttribute("admin");//获取当前管理员用户，插入数据库要用
		String imgname=null;
//		if(!photoFile.isEmpty()) {
//			String prjdir=req.getServletContext().getRealPath("/");
//			String webapps=new File(prjdir).getParent();
//			String ext=photoFile.getOriginalFilename().substring(photoFile.getOriginalFilename().lastIndexOf("."));
//			imgname=UUID.randomUUID().toString()+ext;
//			if(new File(webapps+"/imgs/user/").exists()==false) 
//				new File(webapps+"/imgs/user/").mkdirs();
//			
//			photoFile.transferTo(new File(webapps+"/imgs/user/"+imgname));
//		}
		int n=userService.add(username,userno,userdriverno,userpwd,imgname,userphone,useremail,useraddress);
		if(n==0) {//插入不成功，则提示错误，重新录入
			m.addAttribute("err","没有插入");
			return "admin/admin_input_driver";
		}
		m.addAttribute("success","ok");//添加成功，传信息到页面提示
		return "redirect:/admin/input/Driver";
	}
}
